#include "PopMenuPositionerWidget.h"

#include "Widgets/Window.h"


void RPopMenuPositionerWidget::CalculateChildrenPresentPositionAndSize()
{
    auto Border  =  GetWindow()->GetWindowContentSize();


    for(auto& SubWidget:SubWidgets)
    {

        float Width = SubWidget->Width.Get();
        float Height = SubWidget->Height.Get();
        SubWidget->PresentSize.X = Width;
        SubWidget->PresentSize.Y = Height;

        //subwidget default position is top and left align 
        SubWidget->PresentAbsPosition.X = PresentAbsPosition.X;
        SubWidget->PresentAbsPosition.Y = PresentAbsPosition.Y - Height;

        if(SubWidget->PresentAbsPosition.X +Width> Border.X)
        {
            //make pop menu little left
            SubWidget->PresentAbsPosition.X -=
                SubWidget->PresentAbsPosition.X +Width - Border.X;
        }

        if(SubWidget->PresentAbsPosition.Y < 0)
        {
            //try put subwidget below this widget
            SubWidget->PresentAbsPosition.Y = PresentAbsPosition.Y + PresentSize.Y;
        }


        SubWidget->CalculateChildrenPresentPositionAndSize();
    }

}